<?php
$form = new Form();
$listedPublishers = Product::listPublishers();
$listedCategories = Product::listCategories();
$listedTags = PEGI::listTags();
$publishers = array();
$categories = array();
$ageTags = array();
$warningTags = array();

foreach ($listedTags as $tag)
{
    $tagName = $tag['tagName'];
    if (strstr($tagName, 'Age'))
       $ageTags[$tag['tagId']] = "<img src='{$tag['tagUrl']}' alt='{$tag['tagName']}' />";
    else
       $warningTags[$tag['tagId']] = "<p /><img src='{$tag['tagUrl']}' name='PEGI' alt='{$tag['tagName']}' title='{$tag['tagName']}' />";
}
foreach ($listedPublishers as $publisher)
    $publishers[] = $publisher['publisherName'];
foreach ($listedCategories as $category)
    $categories[] = $category['catName'];
//public function __construct($name, $required, $inputType = null, $dataType = null, $label = null, $optionalData = null, $newLine = true, $container = '')
$form->addInput(new FormInput("title", true, null, null, "Tytuł", array('id' => 'title')));
$form->addInput(new FormSelectInput("catName", true, $categories, array('label' => 'Kategoria')));
$form->addInput(new FormSelectInput("publisherName", true, $publishers, array('label' => 'Wydawca')));
$form->addInput(new FormInput("description", false, "textarea", "string", "Opis", array('id' => 'description')));
$form->addInput(new FormInput("technicalRequirements", false, "textarea", "string", "Wymagania sprzętowe", 
        array('id' => 'technicalRequirements')));
$form->addInput(new FormRegexInput("releaseDate", false, '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])/',
        array('label' => 'Data wydania')));
$form->addInput(new FormRadioInput("PegiAge", true, $ageTags, array('label' => 'Wiek',
                                                                    'checked' => '4', 
                                                                    'container' => '<div class="opcja">%s</div>')));
$form->addInput(new FormCheckboxInput("PegiTag", false, $warningTags, array('label' => 'Oznaczenia', 
                                                                            'container' => "<div class='opcja'>%s</div>\n")));
$form->addInput(new FormInput("metascore", false, "text", "int", "Metascore", array('readonly' => 'true', 'value' => '0', 'id' => 'metascore'), false));
$form->addInput(new FormInput("getMetascoreButton", false, "button", null, "Pobierz Metascore", array('onClick' => 'getMetascore("title", metascore);')));

$form->addInput(new FormInput("videoURL", false, "url", "string", "Adres URL filmu"));
$form->addInput(new FormInput("cover", false, "file", "string", "Okładka", array('accept' => 'image/*')));
$form->addInput(new FormNumberRangeInput("price", false, 1, 10000, 0.01, array('label' => 'Cena')));
$form->addInput(new FormInput("count", false, "number", "numeric", "Ilość sztuk", null, true));
$form->addSubmit("action", "Dodaj grę");
$this->view->form = $form;
$this->view->scripts = array('jquery-1.7.2', 'ajaxfunctions', 'jquery.cleditor.min', 'jquery.tipTip.minified');
$this->contentFile = "Admin/GameAdd";

if (isset($_POST['action']))
{
    $postAction = $_POST['action'];
    unset($_POST['action']);
    if ($postAction == 'Dodaj grę')
    {
        $data = $_POST;
        if (isset($_FILES['cover']) && $_FILES['cover']['name'] != '')
            $data['cover'] = $_FILES['cover'];
        $errors = $form->validateData($data);
        if (!is_array($errors))
        {
            $status = Product::addProduct($data);
            if (is_array($status))
                $this->view->setStatus($status[0], false);
            else
                $this->view->setStatus("Produkt dodany pomyślnie");
        }
        else
        {
            $status = "Formularz zawiera następująca błędy:\n<br/><ul>";
            foreach ($errors as $error)
                $status .= "<li>$error</li>";
            $status .= "</ul>";
            $this->view->setStatus($status, false);
        }
    }
}

?>
